package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.approval.api.ApprovalAllApi;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.model.GsCategoryManage;
import com.sdy.supermarket.biz.model.GsRes;
import com.sdy.supermarket.biz.service.GsCategoryManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;

import java.util.HashMap;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2019-08-05
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/gsCategoryManage")
public class GsCategoryManageController extends BaseController {
    @Autowired
    private GsCategoryManageService gsCategoryManageService;

    @Autowired
    private ApprovalAllApi approvalAllApi;

    /**
     * 获取树形数据
     *
     * @return 树形列表数据
     */
    @GetMapping("/getTree")
    public Response getTreeList() {
        return Response.success(gsCategoryManageService.getTreeList());
    }

    /**
     * 新增分类
     *
     * @param map categoryName aimsId
     * @return
     */
    @PostMapping("/addCategory")
    public Response addCategory(@RequestBody HashMap<String, Object> map) throws BizException {
        String categoryName = (String) map.get("categoryName");
        Assert.isTrue(!StringUtil.isNotBlank(categoryName) && !StringUtil.isNotEmpty(categoryName), "分类名称不可为空");
        GsCategoryManage manage = new GsCategoryManage().setCategoryName(categoryName);
        GsCategoryManage data = gsCategoryManageService.addCategory((Integer) map.get("aimsId"), manage);
        return Response.success(data);
    }

    /**
     * 根据分类表ID获取res表应用列表
     *
     * @param id
     * @return 对应res列表
     */
    @GetMapping("/getResListByCateId")
    public Response getCategoryResList(Integer id, Page<GsRes> page) throws BizException {
        IPage<GsRes> list = gsCategoryManageService.getResList(id, page);
        return Response.success(list);
    }


    /**
     * 根据ID修改该分类信息
     *
     * @param categoryManage
     * @return
     */
    @PostMapping("/updateCategory")
    public Response updateCategory(@RequestBody GsCategoryManage categoryManage) throws BizException {
        int i = gsCategoryManageService.updateCategory(categoryManage);
        return i != 0 ? Response.success("修改成功") : Response.error("修改失败");
    }


    /**
     * 根据ID删除分类数据
     *
     * @return
     */
    @PostMapping("/delById")
    public Response delById(@RequestBody GsCategoryManage gsCategoryManage) throws BizException {
        int i = gsCategoryManageService.delById(gsCategoryManage.getId());
        return i != 0 ? Response.success("删除成功") : Response.error("删除失败");
    }

    /**
     * 修改应用的分类
     *
     * @param map categoryId 目标分类ID resId      应用ID
     * @return
     */
    @PostMapping("/changeResCategory")
    public Response changeResCategory(@RequestBody HashMap<String, Object> map) {
        int i = gsCategoryManageService.changeResCategory((Integer) map.get("resId"), (Integer) map.get("categoryId"));
        return i != 0 ? Response.success("修改成功") : Response.error("修改失败");
    }

    @GetMapping("/getById")
    public Response getById(Integer id) throws BizException {
        Assert.isNull(id, "ID为NULL");
        GsCategoryManage byId = gsCategoryManageService.getById(id);
        return Response.success(byId);
    }

}
